package org.mathig.poker.hand;

import java.util.ArrayList;
import java.util.List;

import org.mathig.poker.Card;
import org.mathig.poker.PokerRank;
import org.mathig.poker.Rank;

public class TwoPair extends AbstractHand {

	protected List<Card> pair1 = new ArrayList<Card>();

	protected List<Card> pair2 = new ArrayList<Card>();

	protected Card kicker;

	public TwoPair(List<Card> pair1, List<Card> pair2, Card kicker) {
		this.pair1 = pair1;
		this.pair2 = pair2;
		this.kicker = kicker;
	}

	public String cards() {
		return pair1 + " " + pair2;
	}

	@Override
	public String toString() {
		return "two pair of " + getRank1() + " and " + getRank2();
	}

	private Rank getRank1() {
		return pair1.get(0).getRank();
	}

	private Rank getRank2() {
		return pair2.get(0).getRank();
	}

	public int compareTo(Hand hand) {
		int compare = super.compareTo(hand);
		if (compare != 0)
			return compare;
		if (hand instanceof TwoPair) {
			TwoPair twoPair = (TwoPair) hand;
			// compare pair1 rank
			compare = getRank1().compareTo(twoPair.getRank1());
			if (compare != 0)
				return compare;
			// compare pair2 rank
			compare = getRank2().compareTo(twoPair.getRank2());
			if (compare != 0)
				return compare;
			// same pair, compare kickers
			return kicker.getRank().compareTo(twoPair.kicker.getRank());
		}
		return compare;
	}

	public PokerRank getPokerRank() {
		return PokerRank.TWO_PAIR;
	}

}
